package com.longxi.tianjintianbao.ui.presenter.patrol;

import android.content.Intent;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.model.patrol.PatrolContent;
import com.longxi.tianjintianbao.ui.activity.patrol.EditPatrolResultsActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.patrol.PatrolContentViewBinder2;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.patrol.IPatrolDetailAtView2;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.NListRefresh;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PatrolDetailAtPresenter2 extends BasePresenter<IPatrolDetailAtView2> {

    private MultiTypeAdapter adapter;
    private Items items = new Items();

    public PatrolDetailAtPresenter2(BaseActivity context) {
        super(context);
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(PatrolContent.class, new PatrolContentViewBinder2(mContext, new PatrolContentViewBinder2.OnSelectPatrolContentListener() {
                @Override
                public void onSuccess(int position) {
                    PatrolContent patrolContent = (PatrolContent) items.get(position);
                    if(patrolContent.getState().equals("")){
                        Intent mainIntent = new Intent(mContext, EditPatrolResultsActivity.class);
                        mainIntent.putExtra(AppConst.BILLDETAILID, patrolContent.getId());
                        mainIntent.putExtra(AppConst.BILLID, mContext.getIntent().getStringExtra(AppConst.BILLID));
                        mContext.startActivityForResult(mainIntent, AppConst.IntentRequstCode.ACTIVITY_COMPLETE_ENTER);
                    }else{
                        MyUtils.showToast("该单据已巡检!");
                    }
                }
            }));
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void getData() {
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("billid", mContext.getIntent().getStringExtra(AppConst.BILLID));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().getInspectionDetail(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<PatrolContent>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<PatrolContent> result) {
                items.clear();
                items = NListRefresh.getInstance().fjData(result,
                        getView().getRefreshLayout(),
                        items,
                        adapter,
                        mContext);
            }
        });
    }

    public void onActivityResult(int requestCode, Intent data) {
        switch (requestCode) {
            case AppConst.IntentRequstCode.ACTIVITY_COMPLETE_ENTER:
                getData();
                break;
        }
    }

}
